package fcl.mylife;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import android.content.Context;
import android.os.Environment;


public class Itinerary {

	ArrayList<Stop> stops = new ArrayList<Stop>();
	ArrayList<Move> moves = new ArrayList<Move>();
	Downloader download = new Downloader();


	public Itinerary(){}
	
	
	public void getPath(){
	
		CSVRead reader = new CSVRead("download.csv");
		try {
			reader.read();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public ArrayList<Stop> getStops(){
		return this.stops;
	}
	
	
	public void reload(){
		stops.clear();
		moves.clear();
		getPath();
	}
	

	class CSVRead {
		
	   

	   String filepath = "F:\\486.csv";
	   
	   public CSVRead(String filename){
		   filepath = filename;
	   }
	   
	   
	   public void test() {
		// TODO Auto-generated method stub
		   
	    
		
	}


	public void read() throws IOException{
		   
		
		String foldername = Environment.getExternalStorageDirectory()
				.getPath() + "/fcl";
		File folder = new File(foldername);

		if (folder == null || !folder.exists()) {
			System.out.println("wrong place~!!!");
		}

		
		File csv = new File(foldername +"/"+filepath);
		
		if (csv.exists()) {
			System.out.print("yes");
		} 
		
		BufferedReader input =  new BufferedReader(new FileReader(csv));
		
		
		   BufferedReader CSVFile = new BufferedReader(new FileReader(Environment
					.getExternalStorageDirectory()+"//fcl//"+filepath));

			  String dataRow = CSVFile.readLine(); // Read first line.
			  // The while checks to see if the data is null. If 
			  // it is, we've hit the end of the file. If not, 
			  // process the data.

			  while (dataRow != null){
			   String[] dataArray = dataRow.split(",");
			   if(dataArray[0].equalsIgnoreCase("link"))
			   {
				   Stop s = new Stop(Double.parseDouble(dataArray[1]),Double.parseDouble(dataArray[2]));
				
				   Stop e = new Stop(Double.parseDouble(dataArray[3]),Double.parseDouble(dataArray[4]));
				   
				   Move m = new Move(s,e);
				   
				   stops.add(s);
				   stops.add(e);
				   
				   m.setDis(Double.parseDouble(dataArray[5]));
				   m.setVehicle(Integer.parseInt(dataArray[6]));
				   m.setstartLocType(Integer.parseInt(dataArray[7]));
				   m.setendLocType(Integer.parseInt(dataArray[8]));
	//			   m.setduration(Long.parseLong(dataArray[9]));
				   m.setStartTime(Long.parseLong(dataArray[10]));
				   m.setEndTime(Long.parseLong(dataArray[11]));
				   
				   //m.setTiemType(dataArray[8]);//DS
				  
				   moves.add(m);
				   //other attributes could be added here
				   
			   }
			   
			   System.out.println(); // Print the data line.
			   dataRow = CSVFile.readLine(); // Read next line of data.
			  }
			  
			  // Close the file once all data has been read.
			  CSVFile.close();

			  // End the printout with a blank line.
			  System.out.println();

		
			} // CSVRead   


	   }


	public ArrayList<Move> getMoves() {
		// TODO Auto-generated method stub
		
		return this.moves;
	}


	public Stop getCenter() {
		// TODO Auto-generated method stub
		if(moves.size() == 0){
			return null;
		}
		
		double sumx = 0;
		double sumy = 0;
		
		for(Move m: moves){
			sumx += (m.getStart().getX()+m.getEnd().getX())/2;
			sumy += (m.getStart().getY()+m.getEnd().getY())/2;
		}
		
		int size = moves.size();
		
		Stop c = new Stop(sumx/size,sumy/size);
		return c;
	}


	public void requestdata( Context c) {
		// TODO Auto-generated method stub
		
		if(c != null){
			download.setContext(c);
		}
	
		download.requestData();
	}

	  
	
}
